package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsThreeFloorOutboundMapper;
import me.zhengjie.pojo.entity.WcsThreeFloorOutbound;
import me.zhengjie.sqlservice.WcsThreeFloorOutboundService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class WcsThreeFloorOutboundServiceImpl extends ServiceImpl<WcsThreeFloorOutboundMapper, WcsThreeFloorOutbound> implements WcsThreeFloorOutboundService {
    private final WcsThreeFloorOutboundMapper wcsThreeFloorOutboundMapper;

    @Override
    public boolean insert(WcsThreeFloorOutbound wcsThreeFloorOutbound) {
        return wcsThreeFloorOutboundMapper.insert(wcsThreeFloorOutbound) >0;
    }

    @Override
    public Integer selectCount() {
        Long size = Long.valueOf(wcsThreeFloorOutboundMapper.selectCount(null));
        return Math.toIntExact(size);
    }

    @Override
    public WcsThreeFloorOutbound selectWorkOrder(String workOrder) {
        LambdaQueryWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsThreeFloorOutboundLambdaQueryWrapper.eq(WcsThreeFloorOutbound::getWorkOrder,workOrder);
        return wcsThreeFloorOutboundMapper.selectOne(wcsThreeFloorOutboundLambdaQueryWrapper);
    }

    @Override
    public boolean updateComplete(int id, Integer completeNumber) {
        LambdaUpdateWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsThreeFloorOutboundLambdaUpdateWrapper.set(WcsThreeFloorOutbound::getCompleteNumber,completeNumber)
                .eq(WcsThreeFloorOutbound::getId,id);
        return wcsThreeFloorOutboundMapper.update(null,wcsThreeFloorOutboundLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsThreeFloorOutbound> selectNoMechanicalArmCompleteList(Integer mechanicalArmComplete) {
        LambdaQueryWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsThreeFloorOutboundLambdaQueryWrapper.eq(WcsThreeFloorOutbound::getMechanicalArmComplete,mechanicalArmComplete)
                .orderByAsc(WcsThreeFloorOutbound::getCreateTime);
        return wcsThreeFloorOutboundMapper.selectList(wcsThreeFloorOutboundLambdaQueryWrapper);
    }

    @Override
    public boolean deleteId(int id) {
        return wcsThreeFloorOutboundMapper.deleteById(id) >0;
    }

    @Override
    public boolean updateMechanicalArmComplete(int id, int mechanicalArmComplete) {
        LambdaUpdateWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsThreeFloorOutboundLambdaUpdateWrapper.set(WcsThreeFloorOutbound::getMechanicalArmComplete,mechanicalArmComplete)
                .eq(WcsThreeFloorOutbound::getId,id);
        return wcsThreeFloorOutboundMapper.update(null,wcsThreeFloorOutboundLambdaUpdateWrapper)>0;
    }

    @Override
    public boolean updateWmsStatus(String workOrder, int wmsStatus) {
        LambdaUpdateWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
       wcsThreeFloorOutboundLambdaUpdateWrapper.set(WcsThreeFloorOutbound::getWmsStatus,wmsStatus)
               .eq(WcsThreeFloorOutbound::getWorkOrder,workOrder);
       return wcsThreeFloorOutboundMapper.update(null,wcsThreeFloorOutboundLambdaUpdateWrapper)>0;
    }

    @Override
    public Integer selectWmsStatusCount(int wmsStatus) {
        LambdaQueryWrapper<WcsThreeFloorOutbound> wcsThreeFloorOutboundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsThreeFloorOutboundLambdaQueryWrapper.eq(WcsThreeFloorOutbound::getWmsStatus,wmsStatus);
        return Math.toIntExact(wcsThreeFloorOutboundMapper.selectCount(wcsThreeFloorOutboundLambdaQueryWrapper));
    }
}
